#define _CRT_SECURE_NO_WARNINGS
#include <chrono>
#include <string>
#include "MemoryPool.h"

using std::string;
using namespace std::chrono;

// 使用内存池的粒子类
struct ParticleWithPool
{
    double x, y, z;
    double lifeTime;

    ParticleWithPool(double a = 0.0, double b = 0.0, double c = 0.0, double time = 0.0)
        : x(a), y(b), z(c), lifeTime(time)
    {
    }

    void* operator new(size_t size);
    void operator delete(void* ptr) noexcept;

    void update()
    {
        lifeTime -= 0.1;
        x += 0.01;
        y += 0.02;
        z += 0.03;
    }
};

// 不使用内存池的粒子类（用于对比）
struct ParticleWithoutPool
{
    double x, y, z;
    double lifeTime;

    ParticleWithoutPool(double a = 0.0, double b = 0.0, double c = 0.0, double time = 0.0)
        : x(a), y(b), z(c), lifeTime(time)
    {
    }

    void update()
    {
        lifeTime -= 0.1;
        x += 0.01;
        y += 0.02;
        z += 0.03;
    }
};

// 定义内存池对象
static MyMemoryPool::FixedSizePool g_ParticlePool(sizeof(ParticleWithPool), 4096);

// 内存池版本的 operator new/delete
void* ParticleWithPool::operator new(size_t size)
{
    return g_ParticlePool.allocate();
}

void ParticleWithPool::operator delete(void* ptr) noexcept
{
    if (ptr) {
        g_ParticlePool.deallocate(ptr);
    }
}

// 性能测试函数
class PerformanceTester
{
public:
    static void test_memory_pool(int object_count, int iteration_count)
    {
        cout << "=== 内存池性能测试 ===" << endl;
        cout << "对象数量: " << object_count << ", 迭代次数: " << iteration_count << endl;

        auto start_time = high_resolution_clock::now();

        // 测试创建和销毁
        auto create_start = high_resolution_clock::now();
        vector<ParticleWithPool*> objects;
        objects.reserve(object_count);

        for (int i = 0; i < object_count; ++i)
        {
            ParticleWithPool* p = new ParticleWithPool(i * 0.1, i * 0.2, i * 0.3, 100.0 + i);
            objects.push_back(p);
        }
        auto create_end = high_resolution_clock::now();

        // 测试业务逻辑性能
        auto update_start = high_resolution_clock::now();
        for (int iter = 0; iter < iteration_count; ++iter)
        {
            for (auto* obj : objects)
            {
                obj->update();
            }
        }
        auto update_end = high_resolution_clock::now();

        // 测试销毁性能
        auto destroy_start = high_resolution_clock::now();
        for (auto* obj : objects)
        {
            delete obj;
        }
        objects.clear();
        auto destroy_end = high_resolution_clock::now();

        auto end_time = high_resolution_clock::now();

        // 计算时间
        auto create_duration = duration_cast<microseconds>(create_end - create_start);
        auto update_duration = duration_cast<microseconds>(update_end - update_start);
        auto destroy_duration = duration_cast<microseconds>(destroy_end - destroy_start);
        auto total_duration = duration_cast<microseconds>(end_time - start_time);

        cout << "创建时间: " << create_duration.count() << " μs" << endl;
        cout << "更新时间: " << update_duration.count() << " μs" << endl;
        cout << "销毁时间: " << destroy_duration.count() << " μs" << endl;
        cout << "总时间: " << total_duration.count() << " μs" << endl;
        cout << endl;
    }

    static void test_standard_new_delete(int object_count, int iteration_count)
    {
        cout << "=== 标准 new/delete 性能测试 ===" << endl;
        cout << "对象数量: " << object_count << ", 迭代次数: " << iteration_count << endl;

        auto start_time = high_resolution_clock::now();

        // 测试创建和销毁
        auto create_start = high_resolution_clock::now();
        vector<ParticleWithoutPool*> objects;
        objects.reserve(object_count);

        for (int i = 0; i < object_count; ++i)
        {
            ParticleWithoutPool* p = new ParticleWithoutPool(i * 0.1, i * 0.2, i * 0.3, 100.0 + i);
            objects.push_back(p);
        }
        auto create_end = high_resolution_clock::now();

        // 测试业务逻辑性能
        auto update_start = high_resolution_clock::now();
        for (int iter = 0; iter < iteration_count; ++iter)
        {
            for (auto* obj : objects)
            {
                obj->update();
            }
        }
        auto update_end = high_resolution_clock::now();

        // 测试销毁性能
        auto destroy_start = high_resolution_clock::now();
        for (auto* obj : objects)
        {
            delete obj;
        }
        objects.clear();
        auto destroy_end = high_resolution_clock::now();

        auto end_time = high_resolution_clock::now();

        // 计算时间
        auto create_duration = duration_cast<microseconds>(create_end - create_start);
        auto update_duration = duration_cast<microseconds>(update_end - update_start);
        auto destroy_duration = duration_cast<microseconds>(destroy_end - destroy_start);
        auto total_duration = duration_cast<microseconds>(end_time - start_time);

        cout << "创建时间: " << create_duration.count() << " μs" << endl;
        cout << "更新时间: " << update_duration.count() << " μs" << endl;
        cout << "销毁时间: " << destroy_duration.count() << " μs" << endl;
        cout << "总时间: " << total_duration.count() << " μs" << endl;
        cout << endl;
    }

    static void run_comprehensive_test()
    {
        cout << "========== 内存池性能对比测试 ==========" << endl;
        cout << endl;

        // 测试不同规模的数据
        vector<int> test_sizes = { 1000, 5000, 10000, 20000 };
        vector<int> iterations = { 10, 50, 100 };

        for (int size : test_sizes)
        {
            for (int iter : iterations)
            {
                cout << "测试规模: " << size << " 个对象, " << iter << " 次迭代" << endl;
                cout << "----------------------------------------" << endl;

                auto pool_start = high_resolution_clock::now();
                test_memory_pool(size, iter);
                auto pool_end = high_resolution_clock::now();

                auto standard_start = high_resolution_clock::now();
                test_standard_new_delete(size, iter);
                auto standard_end = high_resolution_clock::now();

                auto pool_duration = duration_cast<microseconds>(pool_end - pool_start);
                auto standard_duration = duration_cast<microseconds>(standard_end - standard_start);

                double speedup = static_cast<double>(standard_duration.count()) / pool_duration.count();

                cout << "性能对比结果:" << endl;
                cout << "内存池总耗时: " << pool_duration.count() << " μs" << endl;
                cout << "标准分配总耗时: " << standard_duration.count() << " μs" << endl;
                cout << "性能提升倍数: " << speedup << "x" << endl;
                cout << "========================================" << endl;
                cout << endl;
            }
        }
    }
};

int main()
{
    // 运行全面的性能测试
    PerformanceTester::run_comprehensive_test();

    // 额外的简单测试
    cout << "========== 快速测试 ==========" << endl;
    PerformanceTester::test_memory_pool(10000, 100);
    PerformanceTester::test_standard_new_delete(10000, 100);

    // 显示内存池信息
    cout << "内存池信息:" << endl;
    cout << "块大小: " << g_ParticlePool.Get_block_Size() << " 字节" << endl;
    cout << "每页块数: " << g_ParticlePool.Get_block_Per_Page() << endl;

    return 0;
}
